package com.zs.toolbox.web2j.system.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.system.core.entity.WtPostCommentEntity;
import com.zs.toolbox.web2j.system.core.entity.WtPostCommentWithUserInfoEntity;
import com.zs.toolbox.web2j.system.core.entity.WtPostDetailEntity;
import com.zs.toolbox.web2j.system.core.mapper.other.WtPostCommentMapper;
import com.zs.toolbox.web2j.system.core.request.WtCommentPageQueryModel;
import com.zs.toolbox.web2j.system.core.service.WtPostCommentService;
import com.zs.toolbox.web2j.system.core.service.WtPostDetailService;
import com.zs.toolbox.web2j.system.core.service.WtUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author eric.zhang
 * @since 2018-10-11
 */
@Service
public class WtPostCommentServiceImpl extends ServiceImpl<WtPostCommentMapper, WtPostCommentEntity> implements WtPostCommentService {
    @Autowired
    WtUserService wtUserService;

    @Autowired
    WtPostDetailService wtPostDetailService;

    @Override
    public WtPageBodyModel<WtPostCommentWithUserInfoEntity> pageComment(WtCommentPageQueryModel wtPageQueryModel) {
        /*根据ID查询条件，查出对应的记录*/
        LambdaQueryWrapper<WtPostCommentEntity> condition = new LambdaQueryWrapper<>();
        if (null != wtPageQueryModel.getPostId()) {
            condition.eq(WtPostCommentEntity::getCommentPostId, wtPageQueryModel.getPostId());
        }
        if (null != wtPageQueryModel.getParentId()) {
            condition.eq(WtPostCommentEntity::getCommentParent, wtPageQueryModel.getParentId());
        }

        /*查询对象评论*/
        IPage<WtPostCommentEntity> page = baseMapper
                .selectPage(new Page<WtPostCommentEntity>(wtPageQueryModel.getPage(), wtPageQueryModel.getRows()), condition);

        /*对记录填充用户信息*/
        List<WtPostCommentWithUserInfoEntity> records = page.getRecords().stream().map(rcd -> {
            WtPostCommentWithUserInfoEntity entity = convertWtPostCommentEntityAndSetUserinfo(rcd);
            return entity;
        }).collect(Collectors.toList());

        return new WtPageBodyModel<>(page.getTotal(), page.getCurrent(), records);
    }

    @Override
    public WtPostCommentWithUserInfoEntity fetchPostDetailById(Long id) {
        WtPostCommentEntity wtPostCommentEntity = baseMapper.selectById(id);

        if (null == wtPostCommentEntity) {
            throw WtExceptions.E_COMMON_KEY;
        }
        WtPostCommentWithUserInfoEntity entity = convertWtPostCommentEntityAndSetUserinfo(wtPostCommentEntity);
        return entity;
    }

    @Override
    public void saveComment(WtPostCommentEntity wtPostCommentEntity) {
        WtPostDetailEntity postDetailEntity = wtPostDetailService.getById(wtPostCommentEntity.getCommentPostId());
        if (postDetailEntity == null) {
            throw WtExceptions.E_POST_NOEXIST;
        }

        if(0 != wtPostCommentEntity.getCommentParent()) {
            throw WtExceptions.E_POST_COMMENT_NOEXIST;
        }

        baseMapper.insert(wtPostCommentEntity);
    }

    private WtPostCommentWithUserInfoEntity convertWtPostCommentEntityAndSetUserinfo(WtPostCommentEntity entity) {
        WtPostCommentWithUserInfoEntity wtPostCommentWithUserInfoEntity = new WtPostCommentWithUserInfoEntity();
        BeanUtils.copyProperties(entity, wtPostCommentWithUserInfoEntity);

        if (wtPostCommentWithUserInfoEntity.getUserId() > 0) {
            wtPostCommentWithUserInfoEntity
                    .setNickname(wtUserService.getById(entity.getUserId()).getNickname());
        } else {
            wtPostCommentWithUserInfoEntity.setNickname("系统");
        }

        return wtPostCommentWithUserInfoEntity;
    }


}
